<?php
namespace Common\Model;

class UserModel extends CommonModel{
	protected $_validate = array(
   		array('mobile','require','请输入手机号'),
    	array('mobile','/^(1\d{10})$/','手机号格式有误'),
//     	array('mobile','/^(1\d{10})|((0\d{2,3})-)(\d{7,8})(-(\d{3,}))?$/','手机号格式有误'),
    	array('password','require','请输入密码'),
    	array('contact_name','require','请输入收货人姓名'),
    	array('contact_phone','require','请输入联系电话'),
    	array('password','require','请输入密码'),
    );
	/**
	 * 改变成长值
	 * @param unknown $member
	 * @param unknown $cent
	 * @param unknown $msg
	 * @param number $type=1加成长值;$type=2减成长值
	 */
	public function changeCent($member,$cent,$msg,$type = 1){
		$where = array('id'=>$member['id']);
		if($type == 1){
			//增加累积成长值get_cent、目前拥有成长值have_cent
			$this->where($where)->save(array('get_cent'=>$member['get_cent']+$cent,'have_cent'=>$member['have_cent']+$cent));
		}elseif($type==2){
			//扣除目前拥有成长值have_cent、增加已使用成长值used_cent
			$this->where($where)->save(array('used_cent'=>$member['used_cent']+$cent,'have_cent'=>$member['have_cent']-$cent));
		}
		//写成长值记录
		$log_data = array();
		$log_data['customer_id'] = $member['customer_id'];
		$log_data['member_id'] = $member['id'];
		$log_data['nickname'] = $member['nickname'];
		$log_data['message'] = $msg;
		$log_data['type'] = $type;
		$log_data['cent_value'] = $cent;
		D('CentLog')->add($log_data);
	}
	/**
	 * 改变积分商城订单状态
	 * @param unknown $user
	 * @return multitype:number
	 */
	public function state($user){
		$list = D('Order')->where(array('user_id'=>$user))->select();
		$payment = 0; 
		$hair=0; 
		$receipt =0; 
		$complete = 0; 
		$refund = 0;
		foreach ($list as $val) {
			switch ($val['status']) {
				case '1':$payment++;break;
				case '2':$hair++;break;
				case '3':$receipt++;break;
				case '4':$complete++;break;
				case '5':$refund++;break;
			}
		}
		return array('pay'=>$payment,'hair'=>$hair,'receipt'=>$receipt,'complete'=>$complete,'refund'=>$refund);
	}
}